<template>
	<view class="container">
		<view class="content_title">发票信息</view>
		<view class="content">
			<view class="options">
				<view class="title">发票类型</view>
				<view class="right_box selects">
					<view class="select_box" @click="typeClick('1')">
						<view class="select">
							<view class="yes_box" v-if="titleInfo.type == '1'">
								<u-icon name="checkmark-circle-fill" color="#EA3D37" size="46"></u-icon>
							</view>
							<view class="yes_box" v-else>
								<view class="no"></view>
							</view>
						</view>
						<view class="text">
							<view>电子发票</view>
							<view>普通发票</view>
						</view>
					</view>
					<view class="select_box" @click="typeClick('2')">
						<view class="select">
							<view class="yes_box" v-if="titleInfo.type == '2'">
								<u-icon name="checkmark-circle-fill" color="#EA3D37" size="46"></u-icon>
							</view>
							<view class="yes_box" v-else>
								<view class="no"></view>
							</view>
						</view>
						<view class="text">
							<view>电子发票</view>
							<view>增值税专用发票</view>
						</view>
					</view>
				</view>
			</view>
			<view class="options">
				<view class="title">发票类型<text>*</text></view>
				<view class="right_box input_box">
					<u-radio-group v-model="value" @change="radioGroupChange">
						<u-radio @change="radioChange" v-for="(item, index) in list" :key="index" :name="item.name"
							:disabled="item.disabled">
							{{item.name}}
						</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view class="options">
				<view class="title">单位名称<text>*</text></view>
				<view class="right_box input_box">
					<input type="text" placeholder="单位名称(必填)" v-model="titleInfo.company_name" />
				</view>
			</view>
			<view class="options">
				<view class="title">纳税人识别号<text>*</text></view>
				<view class="right_box input_box">
					<input type="text" placeholder="纳税人识别号(必填)" v-model="titleInfo.identification" />
				</view>
			</view>
			<view class="options" v-if="!isMore">
				<view class="title">更多选项</view>
				<view class="right_box more" @click="isMore = true">
					注册地址、电话、开户银行、账号
					<u-icon name="arrow-down"></u-icon>
				</view>
			</view>
			<view class="options" v-if="isMore">
				<view class="title">注册地址<text v-if="titleInfo.type == '2'">*</text></view>
				<view class="right_box input_box">
					<input type="text" placeholder="注册地址(必填)" v-if="titleInfo.type == '2'"
						v-model="titleInfo.reg_address" />
					<input type="text" placeholder="注册地址" v-else v-model="titleInfo.reg_address" />
				</view>
			</view>
			<view class="options" v-if="isMore">
				<view class="title">注册电话<text v-if="titleInfo.type == '2'">*</text></view>
				<view class="right_box input_box">
					<input type="text" placeholder="注册电话(必填)" v-if="titleInfo.type == '2'"
						v-model="titleInfo.reg_phone" />
					<input type="text" placeholder="注册电话" v-else v-model="titleInfo.reg_phone" />
				</view>
			</view>
			<view class="options" v-if="isMore">
				<view class="title">开户银行<text v-if="titleInfo.type == '2'">*</text></view>
				<view class="right_box input_box">
					<input type="text" placeholder="开户银行(必填)" v-if="titleInfo.type == '2'"
						v-model="titleInfo.open_bank" />
					<input type="text" placeholder="开户银行" v-else v-model="titleInfo.open_bank" />
				</view>
			</view>
			<view class="options" v-if="isMore">
				<view class="title">银行账号<text v-if="titleInfo.type == '2'">*</text></view>
				<view class="right_box input_box">
					<input type="text" placeholder="银行账号(必填)" v-if="titleInfo.type == '2'"
						v-model="titleInfo.bank_number" />
					<input type="text" placeholder="银行账号" v-else v-model="titleInfo.bank_number" />
				</view>
			</view>
		</view>

		<view class="content_title info">收件信息<text>（手机号和电子邮箱必填一项）</text></view>
		<view class="content">
			<view class="options">
				<view class="title">手机号</view>
				<view class="right_box input_box">
					<input type="text" placeholder="手机号" v-model="titleInfo.phone" />
				</view>
			</view>
			<view class="options">
				<view class="title">电子邮箱</view>
				<view class="right_box input_box">
					<input type="text" placeholder="电子邮箱" v-model="titleInfo.email" />
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="title">设置默认发票抬头</view>
			<view class="btn_S">
				<view class="text">提醒：每次开票时会使用该抬头,实际下单前请确认好抬头在下单哦!</view>
				<u-switch v-model="checked" active-color="#EA3D37"></u-switch>
			</view>
		</view>
		<view class="agreement" @click="isXy">
			<view class="select">
				<u-icon name="checkmark-circle-fill" color="#EA3D37" size="40" v-if="isConfirm"></u-icon>
				<view class="no" v-else></view>
			</view>
			<view class="text">
				我已阅读并同意
				<text>《增值税专用发票抬头确认书》</text>
			</view>
		</view>
		<view class="submit" @click="submit">
			提交
		</view>
		<u-popup v-model="xyShow" mode="center" border-radius="14">
			<view class="mask_box">
				<view class="mask_title">
					增值税专用发票抬头确认书
				</view>
				<view class="mask_content">
					{{xyContent}}
				</view>
				<view class="btn_box">
					<view class="btn" @click="confirmXy">同意</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isMore: false,
				isConfirm: true,
				xyShow: false,
				checked: false,
				list: [{
						name: '个人',
						value: '1',
						disabled: false
					},
					{
						name: '企业',
						value: '2',
						disabled: false
					}
				],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				value: '个人',
				xyContent: '',
				titleInfo: {
					type: '1', //发票类型 1.普通 2.专用发票
					header_type: '1', //发票类型 1.个人 2.企业
					company_name: '', //单位名称
					identification: '', //纳税人识别号
					reg_address: '', //注册地址-专用发票必须
					reg_phone: '', //注册电话-专用发票必须
					open_bank: '', //开户银行-专用发票必须
					bank_number: '', //银行账号-专用发票必须
					phone: '', //收件手机号-收件信息二选一
					email: '', //电子邮箱-收件信息二选一
					is_default: 0
				},
				phoneZz: /^1(3|4|5|6|7|8|9)\d{9}$/,
				emailZz: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
			}
		},
		methods: {
			radioChange(e) {},
			radioGroupChange(e) {
				this.value = e
				if (e == '个人') return this.titleInfo.header_type = '2';
				else if (e == '企业') return this.titleInfo.header_type = '2'
			},
			confirmXy() {
				this.isConfirm = true
				this.xyShow = false
			},
			isXy() {
				this.$allApi.getBlockInfo({
					type: 'invoice'
				}).then(res => {
					this.xyContent = res.data.data
					this.xyShow = true
				})
			},
			submit() {
				if (this.titleInfo.company_name == '') return this.$u.toast('单位名称不能为空')
				if (this.titleInfo.identification == '') return this.$u.toast('纳税人识别号不能为空')
				if (this.titleInfo.type == '2') {
					if (this.titleInfo.reg_address == '') this.$u.toast('注册地址不能为空')
					if (this.titleInfo.reg_phone == '') return this.$u.toast('注册电话不能为空')
					if (this.titleInfo.open_bank == '') return this.$u.toast('开户银行不能为空')
					if (this.titleInfo.bank_number == '') return this.$u.toast('银行账号不能为空')
				}
				if (this.titleInfo.phone == '' && this.titleInfo.email == '') {
					this.$u.toast('收件手机号和电子邮箱至少填写一个不能为空')
					return false
				}
				if (this.titleInfo.phone !== '' && !(this.phoneZz.test(this.titleInfo.phone))) {
					this.$u.toast('请填写正确的手机号码')
					return false
				}
				if (this.titleInfo.email !== '' && !(this.emailZz.test(this.titleInfo.email))) {
					console.log(this.titleInfo.receive_phone !== '');
					this.$u.toast('请填写正确的电子邮箱')
					return false
				}
				if (!this.isConfirm) return this.$u.toast('请先阅读和同意《增值税专用发票抬头确认书》')
				let is_default = ''
				if (this.checked) {
					is_default = '1'
				} else {
					is_default = '0'
				}
				this.$allApi.addInvoiceTitle(this.titleInfo).then(res => {
					this.$u.toast('添加成功')
					setTimeout(() => {
						this.$backTo()
					}, 1500)
				})
			},
			typeClick(type) {
				this.titleInfo.type = type
				if (type == '1') {
					this.isMore = false
				} else {
					this.isMore = true
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bottom {
		margin-top: 20rpx;
		background: #fff;
		padding: 20rpx;
		color: #666;
		line-height: 1.8;

		.title {
			font-size: 30rpx;
		}

		.btn_S {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.text {
				font-size: 24rpx;
				flex: 1;
				padding-right: 20rpx;
			}
		}
	}

	.mask_box {
		width: 90vw;
		padding: 30rpx;

		.mask_title {
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			padding-bottom: 20rpx;
			margin-bottom: 20rpx;
			border-bottom: 1px solid #eee;
		}

		.mask_content {
			height: 50vh;
			overflow-y: scroll;
			padding-bottom: 20rpx;
			margin-bottom: 80rpx;
			border-bottom: 1px solid #eee;
		}

		.btn_box {
			display: flex;
			align-items: center;
			justify-content: center;

			.btn {
				width: 360rpx;
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				background: #EA3D37;
				border-radius: 14rpx;
			}
		}
	}

	.submit {
		width: 710rpx;
		height: 70rpx;
		border-radius: 35rpx;
		background: #EA3D37;
		left: 20rpx;
		position: fixed;
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;

	}

	.agreement {
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background: #fff;
		padding: 20rpx;
		border-radius: 10rpx;

		.select {
			margin-right: 34rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			.no {
				width: 36rpx;
				height: 36rpx;
				border-radius: 50%;
				border: 1px solid #ccc;
				background: #DCDCDC;
			}
		}

		.text {
			flex: 1;

			text {
				color: red;
			}
		}
	}

	.content {
		background: #fff;
		border-radius: 10rpx;
		padding: 20rpx;

		.options {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-bottom: 20rpx;
			padding-top: 20rpx;
			border-bottom: 1px solid #ECECEC;

			.more {
				font-size: 20rpx;
				color: #999;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.selects {
				display: flex;
				align-items: center;
				justify-content: flex-start;

				.select_box {
					display: flex;
					align-items: center;
					justify-content: flex-start;
					margin-right: 20rpx;

					.text {
						font-size: 20rpx;
					}

					.select {
						margin-right: 20rpx;

						.yes_box {
							width: 60rpx;
							height: 60rpx;
							display: flex;
							align-items: center;
							justify-content: center;
						}

						.no {
							width: 40rpx;
							height: 40rpx;
							border-radius: 50%;
							border: 1px solid #ccc;
							background: #DCDCDC;
						}
					}
				}
			}

			.right_box {
				flex: 1;
				padding-left: 20rpx;
			}

			.title {
				width: 200rpx;
				text-align: left;

				text {
					color: red;
				}
			}
		}

		.options:last-child {
			border: none;
			padding-bottom: 0 !important;
		}

		.options:first-child {
			padding-top: 0 !important;
		}
	}

	.content_title {
		padding-left: 20rpx;
		padding-bottom: 30rpx;
		position: relative;
	}

	.info {
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		justify-content: flex-start;

		text {
			margin-left: 70rpx;
			font-size: 20rpx;
			color: #999;
		}
	}

	.content_title::after {
		content: '';
		width: 6rpx;
		height: 28rpx;
		background: #EA3D37;
		position: absolute;
		left: 0;
		top: 6rpx;
		border-radius: 2rpx;
	}

	.container {
		padding: 30rpx 20rpx;
		min-height: 100vh;
		background: #F8F8F8;
		padding-bottom: 0;
	}
</style>